package com.xb.cloud.merchantapi.adapter;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.ProductPublishApiService;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuInputBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuPicInputBo;

/**
 * 
 * @author: 鲜备科技
 * @date: 2017年12月19日 下午6:30:04
 * 
 * @Copyright: 2017 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class ProductPublishApiAdapter extends Adapter {

	private static final Logger logger = LoggerFactory.getLogger(ProductPublishApiAdapter.class);

	@Autowired
	private ProductPublishApiService productPubishApiService;

	/**
	 * 
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		RtnInfo rtnInfo = null;
		
		/**
		 * 1. 根据类目，获取产品信息summary列表 
		 */ 
		if ("/h5/merchant/productPublish/getPublishableProductSummaryBoListByCategoryId".equalsIgnoreCase(urlPath)) {
			Integer pageNum = null;
			Integer pageSize = null;
			Long categoryId = null;
			/**
			 * 参数验证
			 */
			try {
				pageNum = Integer.parseInt(bizInfo.get("pageNum"));
				pageSize = Integer.parseInt(bizInfo.get("pageSize"));
				categoryId = Long.parseLong(bizInfo.get("categoryId"));
				if (pageNum == null || pageSize == null || categoryId == null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			// 调取Api service
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productPubishApiService.getPublishableProductSummaryBoListByCategoryId(categoryId,currentMerchantUserId, pageNum, pageSize);
			
		}/**
		 * 1.1 根据类目，获取商家下的所有商品模板列表
		 */
		if ("/h5/merchant/productPublish/getPublishableProductSummaryBoListMerchantLevelByCategoryId".equalsIgnoreCase(urlPath)) {
			Integer pageNum = null;
			Integer pageSize = null;
			Long categoryId = null;
			/**
			 * 参数验证
			 */
			try {
				pageNum = Integer.parseInt(bizInfo.get("pageNum"));
				pageSize = Integer.parseInt(bizInfo.get("pageSize"));
				categoryId = Long.parseLong(bizInfo.get("categoryId"));
				if (pageNum == null || pageSize == null || categoryId == null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			// 调取Api service
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productPubishApiService.getPublishableProductSummaryBoListMerchantLevelByCategoryId(categoryId,currentMerchantUserId, pageNum, pageSize);
			
		}else  if ("/h5/merchant/productPublish/getPublishableProductSerialAttrCombBoByProductId".equalsIgnoreCase(urlPath)) {
			/**
			 * 2. 根据系列品虚品id，获取系列组合
			 */
			Long productId; //虚品id
			 
			/**
			 * 参数验证
			 */
			try {
				productId = Long.parseLong(bizInfo.get("productId"));
				if (StringUtils.isEmpty(bizInfo.get("productId"))) { 
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			Long endMerchantUserId=context.getCurrentMerchantUserId();
			// 调取Api service
			rtnInfo = productPubishApiService.getPublishableProductSerialAttrCombBoByProductId(productId, 
					endMerchantUserId);
		}else  if ("/h5/merchant/productPublish/getProductTemplateBoByProductId".equalsIgnoreCase(urlPath)) {
			/**
			 * 3. 根据产品编号，获取待发布产品模板 
			 */
			Byte productType;
			Long productId1;
			Long productId2;
			String seriesAttributeCombProductId;
			/**
			 * 参数验证
			 */
			try {
				productType = Byte.parseByte(bizInfo.get("productType"));
				productId1 = Long.parseLong(bizInfo.get("productId1"));
				productId2 = StringUtils.isEmpty(bizInfo.get("productId2") )?null:Long.parseLong(bizInfo.get("productId2")) ;
				seriesAttributeCombProductId =   bizInfo.get("seriesAttributeCombProductId");
				if (productType == null || productId1 == null ) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			
			// 调取Api service
			rtnInfo = productPubishApiService.getProductTemplateBoByProductId(  productType,   productId1, 
					  productId2,  seriesAttributeCombProductId);
		}else  if ("/h5/merchant/productPublish/createProductSku".equalsIgnoreCase(urlPath)) {
			/**
			 * 4. 发布sku
			 */
			Long endMerchantUserId=null;
			Long productId1=null;
			Long productId2=null; 
			Byte productType=null;
			List<ProductSkuPicInputBo> skuPics=null; 
			String detailContentJson=null;
			ProductSkuInputBo skuInput=null; 
			
			BigDecimal priceMarket=null; 
			BigDecimal priceSale=null; 
			Long stockNum=null; 
			Long warehouseId=null;
			List<Long> groupIdList=null;
			 
			/**
			 * 参数验证
			 */
			try {
				//context.getHttpRequest().getInputStream();
				productId1 = Long.parseLong(bizInfo.get("productId1"));
				productId2 = StringUtils.isEmpty(bizInfo.get("productId2"))?null:Long.parseLong(bizInfo.get("productId2")); 
				productType=Byte.parseByte(bizInfo.get("productType"));
				skuPics= JSONArray.parseArray(bizInfo.get("skuPics"), ProductSkuPicInputBo.class);
				detailContentJson=bizInfo.get("detailContentJson");
				skuInput=JSON.parseObject(bizInfo.get("skuInput"), ProductSkuInputBo.class);
				priceMarket=new BigDecimal(bizInfo.get("priceMarket"));
				priceSale=new BigDecimal(bizInfo.get("priceSale"));
				stockNum=Long.parseLong(bizInfo.get("stockNum"));
				warehouseId=Long.parseLong(bizInfo.get("warehouseId"));
				groupIdList=JSONArray.parseArray(bizInfo.get("groupIdList"), Long.class); 
				
				
				if (StringUtils.isEmpty(bizInfo.get("productId1")) 
						|| StringUtils.isEmpty(bizInfo.get("productType"))
						|| StringUtils.isEmpty(bizInfo.get("skuInput"))
						|| StringUtils.isEmpty(bizInfo.get("priceMarket"))
						|| StringUtils.isEmpty(bizInfo.get("priceSale"))
						|| StringUtils.isEmpty(bizInfo.get("stockNum"))
						|| StringUtils.isEmpty(bizInfo.get("warehouseId"))
						|| StringUtils.isEmpty(bizInfo.get("groupIdList"))){ 
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			  endMerchantUserId=context.getCurrentMerchantUserId();
			// 调取Api service
			rtnInfo = productPubishApiService.createProductSku(endMerchantUserId, productId1, productId2, productType,
					skuPics, detailContentJson, skuInput, priceMarket, priceSale, stockNum,
					warehouseId, groupIdList);
		}

		
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/merchant/productPublish/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}
}
